

/**WebSocket前端控制器
 * */

$.post({
    type:"POST",
    url:"/user/getUserParam",
    contentType:"application/json;charset=utf-8",
    dataType:"json",
    success:function (result){
        if (result.flag){
            webSocket(result.data.uname);
        }
    }


})


function webSocket(userName) {
    var websocket = null;
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://localhost:8080/websocket/"+userName);
    } else {
        let message= "刚刚";
        var html = '<div class="toast" role="alert"  data-delay="50000" aria-live="assertive" aria-atomic="true" >'
            +'       <div class="toast-header">'
            +'         <img height="25px" width="25px" src="/static/Img/PageImg/Canva_small.png" class="rounded mr-2" alt="">'
            +'         <strong class="mr-auto" style="color: orange">链接失败</strong>'
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +'         <small class="text-muted">'+message+'</small>'
            +'         <button type="button" class="ml-2 mb-1 close" onclick="deleteToast()" data-dismiss="toast" aria-label="Close">'
            +'           <span aria-hidden="true">&times;</span>'
            +'         </button>'
            +'       </div>'
            +'       <div class="toast-body">'
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +           "当前浏览器不支持消息中心组件！"
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +'       </div>'
            +'     </div>';
        $("#message_Div").append(html)
        $('.toast').toast("show")
    }
    websocket.onerror = function () {
        let message= "刚刚";
        var html = '<div class="toast" role="alert"  data-delay="50000" aria-live="assertive" aria-atomic="true" >'
            +'       <div class="toast-header">'
            +'         <img height="25px" width="25px" src="/static/Img/PageImg/Canva_small.png" class="rounded mr-2" alt="">'
            +'         <strong class="mr-auto" style="color: orange">发送失败</strong>'
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +'         <small class="text-muted">'+message+'</small>'
            +'         <button type="button" class="ml-2 mb-1 close" onclick="deleteToast()" data-dismiss="toast" aria-label="Close">'
            +'           <span aria-hidden="true">&times;</span>'
            +'         </button>'
            +'       </div>'
            +'       <div class="toast-body">'
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +           "消息发送失败！"
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +'       </div>'
            +'     </div>';
        $("#message_Div").append(html)
        $('.toast').toast("show")
    }
    websocket.onopen = function (data) {
        let url = window.location.href.pathname
        let pageUrl = getPageUrl()

        if (url === pageUrl) {
            onOpenFindMessage(data)
        }
    }
    websocket.onmessage = function (data) {
        onOpenFindMessage(data)

    }
    websocket.onclose = function () {

    }
    window.onbeforeunload = function () {
        clos();
    }

    function clos() {
        websocket.close(3000, "强制关闭");
    }


    /*function send() {
        var msg = document.getElementById('text').value;
        websocket.send(msg);
    }*/
}

function deleteToast(){
    $('.toast').remove()
}

function messageCenter() {
    window.location.href = "../MessageCenter.html"
}

function onOpenFindMessage(data) {
    var json = JSON.parse(data.data)




    let month = json.month
    let day = json.day
    let hour = json.hour
    let minute = json.minute
    let date = month + "月" + day + "日" + hour + ":" + minute

    if (json.importantNewsMessageEntity){
        $("#importantNewsMessageEntityText").text(json.mongoDB_s_c_webSocketMessageEntity.importantNewsMessage.importantNewsMessage)
        $("#importantNewsMessageEntity").removeAttr("hidden")
    }else {
        var html = '<div class="toast" role="alert"  data-delay="50000" aria-live="assertive" aria-atomic="true"  >'
            +'       <div class="toast-header">'
            +'         <img height="25px" width="25px" src="/static/Img/PageImg/Canva_small.png" class="rounded mr-2" alt="">'
            +'         <strong class="mr-auto">消息提醒</strong>'
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +'         <small class="text-muted">'+date+'</small>'
            +'         <button type="button" class="ml-2 mb-1 close" onclick="deleteToast()" data-dismiss="toast" aria-label="Close">'
            +'           <span aria-hidden="true">&times;</span>'
            +'         </button>'
            +'       </div>'
            +'       <div class="toast-body" onclick="messageCenter()">'
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +           json.message
            +'          &nbsp;&nbsp;&nbsp;&nbsp;'
            +'       </div>'
            +'     </div>';
        if (json.messageCount > 0){
            $("#message_Div").append(html)
            $('.toast').toast("show")
            if (json.messageCount < 100) {
                $("#messageCount").text(json.messageCount)
            }else {
                $("#messageCount").text("99+")
            }
        }


    }



}


